# Zope imports
from AccessControl import ClassSecurityInfo

# zope imports
from zope.interface import implements

# ATContentTypes imports
from Products.ATContentTypes.configuration import zconf

# Archetypes imports

from Products.Archetypes.atapi import *
from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import *

# EasyArticle imports
from easyarticle.default.config import *
from easyarticle.default.interfaces import IEAReferenceContent

schema = Schema((
    TextField('text',
              required=False,
              searchable=True,
              primary=True,
              storage = AnnotationStorage(migrate=True),
              validators = ('isTidyHtmlWithCleanup',),
              #validators = ('isTidyHtml',),
              default_content_type = zconf.ATDocument.default_content_type,
              default_output_type = 'text/x-html-safe',
              allowable_content_types = zconf.ATDocument.allowed_content_types,
              widget = RichWidget(
                        description = "",
                        description_msgid = "help_body_text",
                        label = "Body Text",
                        label_msgid = "label_body_text",
                        rows = 25,
                        i18n_domain = "EasyArticle",
                        allow_file_upload = zconf.ATDocument.allow_document_upload)),
    ReferenceField( 
        name='objectReference', 
        required=True,
        multiValued=0,
        relationship='easyarticle_object',
        widget=ReferenceBrowserWidget(        
            label='Products',
            label_msgid='schema_object_reference_label',
            description='',
            description_msgid="schema_object_reference_description",
            i18n_domain='EasyArticle',                    
            show_path=1,
            allow_search=1, 
            allow_browse=1,
            available_indexes={'SearchableText':'Free text search'},
            ),    
    ),    
),
)

schema = BaseSchema.copy() + schema.copy()
schema["title"].required = False

class EAReference(BaseContent):
    """A reference for EasyArticle
    """
    implements(IEAReferenceContent)
    security = ClassSecurityInfo()
    _at_rename_after_creation = True
    schema = schema
    
    def SearchableText(self):
        """
        """
        # Not this object will be unindex by the parent article
        # After the parent article has taken the SearchableText
        try:
            return self.getObjectReference().SearchableText()
        except AttributeError:
            return ""
            
    def reindexObject(self, idxs=[]):
        """
        """
        # Parent articel takes SearchableText and unindex this object
        self.aq_parent.reindexObject(idxs)

registerType(EAReference, PROJECTNAME)